#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "database.h"
#include "request.h"

struct database *create_database () {
	struct database *db = malloc (sizeof (struct database));
	db->list_peer = NULL;
	db->nb_peer = 0;
	return db;
}

struct peer *init_peer () {
	struct peer *p = malloc (sizeof (struct peer));
	p->ip = malloc(SIZE_IP);
	p->port = malloc(SIZE_PORT);
	p->list_file = NULL;
	p->nb_file = 0;
	return p;
}

struct file *init_file () {
	struct file *f = malloc (sizeof (struct file));
	f->name = malloc(SIZE_STR_FIELD);
	f->size = malloc(SIZE_STR_FIELD);
	f->block_size = malloc(SIZE_STR_FIELD);
	f->key = malloc(SIZE_STR_FIELD);
	f->is_seed = 0;
	return f;
}

struct peer *create_peer (char *ip, char *port) {
	struct peer *p = init_peer ();
	strcpy(p->ip, ip);
	strcpy(p->port, port);
	return p;
}

struct file *create_file (char *name, char *size, char *block_size, char *key, int is_seed) {
	struct file *f = init_file ();
	strcpy(f->name, name);
	strcpy(f->size, size);
	strcpy(f->block_size, block_size);
	strcpy(f->key, key);
	f->is_seed = is_seed;
	return f;
}
	
struct peer* find_peer (struct peer *p_search) {
	if (db == NULL) return 0;
	int i;
	struct peer *p;
	for (i=0; i<db->nb_peer; i++) {
		p = db->list_peer[i];
		if (strcmp(p->ip, p_search->ip) == 0 && strcmp(p->port, p_search->port) == 0)
			return p;
	}
	return NULL;
}

struct peer * find_peer_with_ip (struct ip_and_socket i_and_s) {
	int i;
	struct peer * p;
	for (i=0; i<db->nb_peer; i++) {
		p = db->list_peer[i];
		if (equal (p->ip, i_and_s.ip))
			return p;
	}																																																																																																																																																																																																																																																																																																																																																
	return NULL;
}

struct file * find_file (char * key) {
	int i, j;
	struct peer * p;
	struct file * f;
	
	for (i=0; i<db->nb_peer; i++) {
		p = db->list_peer[i];
		if (p->list_file != NULL) {
			for (j=0; j<p->nb_file; j++) {
				f = p->list_file[j];
				if (equal (f->key, key)) 
					return f;
			}
		}
	}
	return NULL;
}

void add_file_to_peer (struct file *f, struct peer *p) {
	p->list_file = realloc (p->list_file, sizeof(struct file*) * (p->nb_file + 1));
	p->list_file[p->nb_file] = f;
	p->nb_file+=1;
}

void add_peer_to_database (struct peer *p) {
	db->list_peer = realloc (db->list_peer, sizeof(struct peer*) * (db->nb_peer + 1));
	db->list_peer[db->nb_peer] = p;
	db->nb_peer+=1;
}

void print_database () {
	if (db == NULL) return;
	int i;
	struct peer *p;
	for (i=0; i<db->nb_peer; i++) {
		p = db->list_peer[i];
		printf ("\t%s:%s\n", p->ip, p->port);
		if (p->list_file != NULL) {
			int j;
			struct file *f;
			for (j=0; j<p->nb_file; j++) {
				f = p->list_file[j];
				printf ("\t\t%s %s %s %s ", f->name, f->size, f->block_size, f->key);
				if (f->is_seed) printf("seed\n");
				else printf("leech\n");
			}
		}
	}
	putchar('\n');
}
